﻿var trunPageObj;//翻页组件对像
var fromEleIds = "#secCode,#allCode,#startDate,#endDate";
var isModify;//是否修改
$(initPage);
function initPage() {
	
		
	//新增
	$("#btnAdd").click(function() {
		$("#btnAdd,#btnEdit,#btnDelete").attr("disabled", true);
		$("#btnSave,#btnCancle").attr("disabled", false);
		$(fromEleIds).attr("disabled", false);
		isModify = false;
		clearValues();
	});
	//修改
	$("#btnEdit").click(function() {
		$("#btnEdit").attr("disabled", true);
		$("#btnSave,#btnCancle").attr("disabled",false);
		$(fromEleIds).attr("disabled",false);
		isModify = true;
	});
	
	//取消
	$("#btnCancle").click(function() {
		$("#btnEdit,#btnDelete,#btnSave,#btnCancle").attr("disabled", true);
		$("#btnAdd").attr("disabled",false);
		$(fromEleIds).attr("disabled", true);
		clearValues();
	});
	
	//保存
	$("#btnSave").click(function() {
		saveAllotmentConf();
	});
		//删除
	$("#btnDelete").click(function() {
		delAllotmentConf();
	});
	//初始化翻页组件
    trunPageObj =  $(".pageNext").turnPage();
	trunPageObj.gotoPage(1);//搜索第一页
}

//清除表单值
function clearValues() {
	$("#busiName").val("");
	$(".enableFlag").attr("checked", false);
	$("#confValueTD").html("<select id='confValue' class='select-one select-width' disabled><option value=''>　　　</option></select>");
	$("tr","#data_list").attr("bgColor","#FFFFFF");
}

//保存
function saveAllotmentConf() {
	var startDate = $.trim($("#startDate").val());
	var endDate = $.trim($("#endDate").val());
	if ($.compareDate(startDate, endDate)) {
		alert("开始日期不能大于结束日期");
		return;
	}
	
	if($.validInput("secCode", "证券代码", true, "", 20)){
		return false;
	}
	if($.validInput("allCode", "配股代码", true, "", 20)){
		return false;
	}
	if($.validInput("startDate", "配股开始日期", true, "\<\>\'\"", 10)){
		return false;
	}
	if($.validInput("endDate", "配股截止日期", true, "\<\>\'\"", 10)){
		return false;
	}
	
	var action = isModify ? "modify" : "add";
	$.post("m/allot_conf/" + action, {
		    secCode :$.trim($("#secCode").val()),
			allCode :$.trim($("#allCode").val()),
			confId :$.trim($("#confId").val()),
			startDate :startDate,
			endDate : endDate
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
				alert("系统提示：保存成功！");
				$("#btnCancle").click();
				queryList();
			} 
		}, "json");
}
//删除
function delAllotmentConf() {
	if (!confirm("确定要删除记录吗？")) {
		return;
	}
	$.post("m/allot_conf/delete", {
		    confId :$.trim($("#confId").val())
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
				$("#btnCancle").click();
				queryList();
			} 
		}, "json");
}
function queryList() {
	var pageno=trunPageObj.getCurrentPageNo();//当前第几页
	var pagecount=trunPageObj.getPageCount();//一页多少条
	$.post("m/allot_conf/query", {
		pageNo: pageno,
		pageSize : pagecount
	}, function(data) {
		if ($.checkErrorMsg(data) ) {
			var fileList = "";
			if(data.allotmentConfPage && data.allotmentConfPage.dataList){
				var dataList = data.allotmentConfPage.dataList;
				$(dataList).each(function(i) {
					fileList += "<tr class='table_td_font' bgColor='#ffffff' onclick='selectTR(this)'>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap value=" + dataList[i].confId + ">" + (i + 1) + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap value=" + dataList[i].secCode + ">" + dataList[i].secCode + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap value=" + dataList[i].allCode + ">" + dataList[i].allCode + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap value=" + dataList[i].formatStartDate + ">" + dataList[i].formatStartDate + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap value=" + dataList[i].formatEndDate + ">" + dataList[i].formatEndDate + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].formatOperDate + " " +dataList[i].formatOperTime + "</td>";
					fileList += "</tr>";
				});
			}
			$("#data_list").html(fileList);
			trunPageObj.setPageData(data.allotmentConfPage);
		} 
	}, "json");
}

function selectTR(obj) {
	$(obj).attr("bgColor","#FAEBD7").siblings().attr("bgColor","#FFFFFF");
	var tds = $(obj).find("td");
	$("#confId").val(tds.eq(0).attr("value"));
	$("#secCode").val(tds.eq(1).text());
	$("#allCode").val(tds.eq(2).text());
	$("#startDate").val(tds.eq(3).text());
	$("#endDate").val(tds.eq(4).text());
	$("#btnSave,#btnCancle").attr("disabled", true);
	$("#btnAdd,#btnEdit,#btnDelete").attr("disabled", false);
	$(fromEleIds).attr("disabled", true);
}

function  sortObj(nodeId, flag) {
	$.post("m/submit_flow/sort", {
			nodeId:nodeId,
			sortUp:flag
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
				$("#btnCancle").click();
				$("#data_list").html("");
				queryList();
			} 
		}, "json");
	event.cancelBubble=true;
}